package com.itxiaoxiong.chat.controller;

import com.itxiaoxiong.chat.mapper.GroupMapper;
import com.itxiaoxiong.chat.mapper.UserMapper;
import com.itxiaoxiong.chat.pojo.*;
import com.itxiaoxiong.chat.service.GroupService;
import com.itxiaoxiong.chat.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.*;

@Slf4j
@RestController
public class GropController {
    @Autowired
    private GroupService groupService;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private UserMapper userMapper;

    /*
    根据用户id查找所在群列表
     */
    @GetMapping("/getGroups")
    public Result getGroups(@RequestParam Integer userId){
        List<GroupMembers> groups = groupService.getGroups(userId);
        List<Groups> groupList = new ArrayList<>();
        for (GroupMembers group : groups){
            if(group.getUserId() == userId){
                Groups groupss = groupMapper.getById(group.getGroupId());
                groupList.add(groupss);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("groups", groupList);
        return Result.success(result);
    }

    /*
    查询聊天记录
     */
    @GetMapping("/groupChatSelect")
    public Result groupChatSelect(@RequestParam Integer userId, @RequestParam Integer groupId){
        boolean exists = groupMapper.checkIfFriendExists(userId, groupId);
        if (exists){
            List<GroupMessages> messages = groupService.groupChatSelect(groupId);
            List<Integer> members = groupService.getMemberByGroupId(groupId);
            List<User> users = new ArrayList<>();
            for (Integer member : members) {
                User user = userMapper.listUserName(member);
                users.add(user);
            }
            Map<String, Object> result = new HashMap<>();
            result.put("member",users);
            result.put("message", messages);
            return Result.success(result);
        }else{
            return Result.error("不在这个群");
        }
    }

    /*
    发消息
     */
    @PostMapping("/chatGroupAdd")
    public Result groupAddChat(@RequestBody GroupMessages groupMessages){
        log.info("发消息: {}", groupMessages);
        groupService.groupAddChat(groupMessages);
        return Result.success();
    }

    /*
    根据群名查找群
     */
    @GetMapping("/chatGroupName")
    public Result groupByName(@RequestParam String groupName){
        log.info("根据群名查找:{}", groupName);
        Groups groups = groupService.groupByname(groupName);
        return Result.success(groups);
    }


    /*
    加群
     */
    @PostMapping("/groupAdd")
    public Result groupAdd(@RequestBody GroupMembers groupMembers){
        boolean exists = groupMapper.checkIfFriendExists(groupMembers.getUserId(),groupMembers.getGroupId());
        if(!exists){
            groupService.groupAdd(groupMembers);
            return Result.success();
        }else{
            return Result.error("已经在群聊中，不能重复添加");
        }
    }

    /*
    退群
     */
    @DeleteMapping("/groupDelete")
    public Result groupDelete(@RequestBody GroupMembers groupMembers){
        boolean exists = groupMapper.checkIfFriendExists(groupMembers.getUserId(),groupMembers.getGroupId());
        if(exists){
            groupService.groupDelete(groupMembers);
            return Result.success();
        }else{
            return Result.error("不在群聊中");
        }
    }

    /*
    发文件
     */
    @PostMapping("/addGroupFile")
    public Result addGroupFile(@RequestBody GroupMessages groupMessages){
        log.info("发送图片:{}", groupMessages);
        groupService.addGroupFile(groupMessages);
        return Result.success();
    }

}

